package cn.com.nes.site.action.templetedesign;


import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.ParameterUtil;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.templatedesign.HHousemodelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/houseModel")
public class HHousemodelController {

    @Resource
    private HHousemodelService housemodelService;

    /**
     * 添加模板
     * 已联调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/addHouseModel")
    @ResponseBody
   public PageData addHouseModel(HttpServletRequest request,HttpServletResponse response) throws IOException {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        List<Map<String,Object>> list=housemodelService.selectByModelName(req);
        if (list.size()==0){
            res = housemodelService.addHouseModel(req);
            res.setRetCode(0);
            res.setRetMessage("操作成功!");
        }
        return res;
    }

    /**
     * 编辑模板
     * 已联调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/updateHouseModel")
    @ResponseBody
    public PageData updateHouseModel(HttpServletRequest request,HttpServletResponse response){
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;

        }
        DataReq req = req_Status.getReq();
       int count= housemodelService.updateHouseModel(req);
       if (count!=0){
           res.setRetCode(0);
           res.setRetMessage("操作成功!");
       }
        return res;
    }

    /**
     * 删除模板 isdelete 0 为未删除，1 为已删除
     *已联调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/deleteHouseModel")
    @ResponseBody
    public PageData deleteHouseModel(HttpServletRequest request,HttpServletResponse response){
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
       int count= housemodelService.deleteHouseModel(req);
       if (count!=0){
           res.setRetCode(0);
           res.setRetMessage("操作成功!");
           return res;
       }
        return res;
    }


    /**
     * 根据类型和名字查询模板或单独根据类型或名字查询模板或直接无条件查询
     * 已联调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/selectByModelNameorType")
    @ResponseBody
    public PageData selectByModelNameorType(HttpServletRequest request,HttpServletResponse response){
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        List<Map<String,Object>> list=housemodelService.selectByModelNameorType(req);
        if (list.size()!=0){
         res.setRetCode(0);
         res.setRetMessage("操作成功!");
        }else {
            res.setRetCode(0);
            res.setRetMessage("OK");
        }
        int recordCount=housemodelService.selectALLlist(req);
        res.setRecordcount(recordCount);
        int pageSize=ParameterUtil.getInteger(req,"pagesize");
        if(pageSize!=0){
            int pageCount=recordCount/pageSize+1;
            res.setPagecount(pageCount);
        }else {
            res.setRetMessage("pageSize为0！");
            return res;
        }
        res.setPage(list);
        return res;
    }

}
